import sys,re
    
#遍历文件行的生成器
def lines(file):
    for line in open(file):
        yield line
    #最后加上换行
    yield '\n'

#遍历行并将行组装成一个字符串
def blocks(file):
    block=[]
    for line in lines(file):
        if line.strip():#不为空行
            block.append(line)
        elif block:
            yield ''.join(block).strip()
            block=[]
            
def parse(file):
    html=[]
    html.append('<html><head><title>...</title><body>')
    title=True
    for block in blocks(file):
        #匹配*sss*这种，替换成<em>sss</em>
        block=re.sub(r'\*(.+?)\*',r'<em>\1</em>',block)
        if title:#第一句作为标题
            html.append('<h1>')
            html.append(block)
            html.append('</h1>')
            title=False
        else:
            html.append('<p>')
            html.append(block)
            html.append('</p>')
    html.append('</body></html>')
    return ''.join(html).strip()

if __name__=='__main__':
    print(parse('test_input.txt'))
    with open('output.html','w') as f:
        f.write(parse('test_input.txt'))
